package ilvonen.ihstatistics.repository;

import ilvonen.ihstatistics.models.Roster;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

public class DBDefaultRoster {
	//define table name
		private static final String TABLE_NAME = "default_roster";
	//define tables column names
		private static final String KEY_ID = "_id";
		private static final String KEY_TEAM_ID = "team_id";
		private static final String KEY_PLAYER_ID = "player_id";
		private static final String KEY_LINE_ID = "line_id";
		private static final String KEY_POSITION_ID = "position_id";
		private static final String KEY_SLEEVE_NRO = "sleeve_number";
	//define create statement for all your tables. Create new file (class) for each table.
		private static final String CREATE_STATEMENT = "create table if not exists " + TABLE_NAME + " (" 
														+ KEY_ID + " integer primary key autoincrement, "
														+ KEY_TEAM_ID + " integer not null, "
														+ KEY_PLAYER_ID + " integer not null, "
														+ KEY_LINE_ID + " integer not null, "
														+ KEY_POSITION_ID + " integer not null, "
														+ KEY_SLEEVE_NRO + " integer not null);";
	
	/*
	 * CLASS STATUS: READY!
	 */
	public DBDefaultRoster(){}
	/*
	 * 
	 */
	public String create(){
		return CREATE_STATEMENT;
	}
	/*
	 * 
	 */
	public long insert(int teamid, int playerid, int lineid, int positionid, int sleevenro, DBAdapter db){
		
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_TEAM_ID, teamid);
		initialValues.put(KEY_PLAYER_ID, playerid);
		initialValues.put(KEY_LINE_ID, lineid);
		initialValues.put(KEY_POSITION_ID, positionid);
		initialValues.put(KEY_SLEEVE_NRO, sleevenro);
		
		return db.insertRecord(TABLE_NAME, initialValues);
		
	}
	/*
	 * 
	 */
	public boolean delete(long rowId, DBAdapter db){
		return db.deleteRecord(TABLE_NAME, KEY_ID, rowId);
	}
	
	/*
	 * 
	 */
	public List<Roster> getDefaultRosterByTeamId(DBAdapter db, long ROW_ID){
		Cursor c;
		List<Roster> roster = new ArrayList<Roster>();
		c = db.getAllRecordsById(TABLE_NAME, new String[]{KEY_ID,KEY_TEAM_ID,KEY_PLAYER_ID,KEY_LINE_ID,KEY_POSITION_ID,KEY_SLEEVE_NRO}, KEY_TEAM_ID, ROW_ID);
		while(c.moveToNext()){
			Roster r = new Roster();
			r.set_id(c.getInt(0));
			r.set_teamId(c.getInt(1));
			r.set_playerId(c.getInt(2));
			r.set_lineId(c.getInt(3));
			r.set_positionId(c.getInt(4));
			r.set_sleeveNumber(c.getInt(5));
			roster.add(r);
		}
		return roster;
	}
}
